import { defineStore } from "pinia";
import { store } from "../utils";

// Define default test user ID
const DEFAULT_TEST_USER_ID = '0195ccb8-cbb2-766a-b7f5-f01b995daeb6';

export interface ApartmentUserState {
  userId: string;
  username: string;
  nickname?: string;
  avatar?: string;
  token?: string;
}

export const useApartmentUserStore = defineStore({
  id: "apartment-user",
  state: (): ApartmentUserState => ({
    userId: DEFAULT_TEST_USER_ID,
    username: 'testuser',
    nickname: '',
    avatar: '',
    token: ''
  }),
  actions: {
    /**
     * Set user ID
     */
    setUserId(id: string) {
      this.userId = id;
    },
    
    /**
     * Reset user ID to default test ID
     */
    resetToDefaultUserId() {
      this.userId = DEFAULT_TEST_USER_ID;
    },

    /**
     * Set user information
     */
    setUserInfo(userInfo: Partial<ApartmentUserState>) {
      if (userInfo.userId) this.userId = userInfo.userId;
      if (userInfo.username) this.username = userInfo.username;
      if (userInfo.nickname) this.nickname = userInfo.nickname;
      if (userInfo.avatar) this.avatar = userInfo.avatar;
      if (userInfo.token) this.token = userInfo.token;
    },

    /**
     * Clear user information
     */
    clearUserInfo() {
      this.userId = DEFAULT_TEST_USER_ID;
      this.username = 'testuser';
      this.nickname = '';
      this.avatar = '';
      this.token = '';
    }
  },
  // Enable persistedstate plugin for this store
  persist: {
    key: 'apartment-user',
    storage: localStorage
  }
});

export function useApartmentUserStoreHook() {
  return useApartmentUserStore(store);
}

// Export default test user ID for convenience
export { DEFAULT_TEST_USER_ID }; 